using System;
using System.Collections;
using System.IO;
using System.Windows.Forms;

namespace Patchy
{
    /// <summary>
    /// Summary description for CreatePatchFiles.
    /// </summary>
    public class CreatePatchFiles
    {
        public CreatePatchFiles(PatchDetails details, ArrayList types)
        {
            ParseFile parser = new ParseFile(details.PatchFilePath);
            GroupHolder groupHolder = parser.ReadFile();
            if (groupHolder.IsEmpty())
            {
                return;
            }
            MinFileName minFileNames = new MinFileName(groupHolder);
            minFileNames.Process();
            MinDirName minDirNames = new MinDirName(groupHolder);
            minDirNames.Process();
            groupHolder.Process();

            bool copyRequested = false;
            FolderBrowserDialog folderBrowser = new FolderBrowserDialog();
            folderBrowser.Description = "Select the directory to export to.";
            folderBrowser.SelectedPath = Path.GetDirectoryName(details.PatchFilePath);
            if (!groupHolder.IsEmpty() && folderBrowser.ShowDialog() == DialogResult.OK)
            {
                string targetDir = "";
                targetDir = Path.Combine(folderBrowser.SelectedPath, details.PatchBaseName);
                if (!Directory.Exists(targetDir))
                {
                    Directory.CreateDirectory(targetDir);
                }
                string report = "";
                foreach (CreateFile t in types)
                {
                    t.Init(details, groupHolder);
                    copyRequested |= t.RequestsCopy();
                    report += t.Write(targetDir);
                }
                MessageBox.Show(report);
                if (copyRequested)
                {
                    FileOperations fileOps = new FileOperations();
                    fileOps.DoCopy(targetDir, groupHolder);
                }
            }
        }
    }
}
